﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;

namespace CJJCH.Web.Manage
{
    public partial class StandardsEdit : Web.Manage.PowerBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitControl();
                InitRequest();
                InitDataBind();
            }
            LTP.Common.ButtonClass.ButtonOnClick(this.Page, btnSave, "checkform()", false);
        }
        private void InitRequest()
        {

            if (!string.IsNullOrEmpty(Request.QueryString["orderkey"]))
            {
                lblOrderKey.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["orderkey"].Trim());
            }
            if (!string.IsNullOrEmpty(Request.QueryString["ordertype"]))
            {
                lblOrderType.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["ordertype"].Trim());
            }
            if (!string.IsNullOrEmpty(Request.QueryString["keyword"]))
            {
                lblKeyword.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["keyword"].Trim());
            }
            if (!string.IsNullOrEmpty(Request.QueryString["keytype"]))
            {
                lblKeytype.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["keytype"].Trim());
            }
            if (!string.IsNullOrEmpty(Request.QueryString["betime"]))
            {
                lblTimeBegin.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["betime"].Trim());
            }
            if (!string.IsNullOrEmpty(Request.QueryString["edtime"]))
            {
                lblTimeOver.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["edtime"].Trim());
            }

            if (Request.QueryString["curpage"] != null && LTP.Common.CheckInput.CheckDigit(Request.QueryString["curpage"].Trim()))
            {
                lblcurpage.Text = Request.QueryString["curpage"];
            }
            else
            {
                lblcurpage.Text = "1";
            }
            if (!string.IsNullOrEmpty(Request.QueryString["secondname"]))
            {
                lblSecondName.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["secondname"].Trim());
                lnkStandards.Text = lblSecondName.Text + "列表";
                lnkStandards.NavigateUrl = "StandardsList.aspx?curpage=" + LTP.Common.StringClass.EncodeUrl(lblcurpage.Text.Trim())
                       + "&keyword=" + LTP.Common.StringClass.EncodeUrl(lblKeyword.Text.Trim())
                       + "&orderkey=" + LTP.Common.StringClass.EncodeUrl(lblOrderKey.Text.Trim())
                       + "&ordertype=" + LTP.Common.StringClass.EncodeUrl(lblOrderType.Text.Trim())
                       + "&keytype=" + LTP.Common.StringClass.EncodeUrl(lblKeytype.Text.Trim())
                       + "&betime=" + LTP.Common.StringClass.EncodeUrl(lblTimeBegin.Text.Trim())
                       + "&edtime=" + LTP.Common.StringClass.EncodeUrl(lblTimeOver.Text.Trim())
                       + "&secondid=" + LTP.Common.StringClass.EncodeUrl(lblSecondId.Text.Trim())
                       + "&firstid=" + LTP.Common.StringClass.EncodeUrl(lblFirstID.Text.Trim())
                       + "&secondname=" + LTP.Common.StringClass.EncodeUrl(lblSecondName.Text.Trim());

            }

        }
        private void InitDataBind()
        {
            if (Request.QueryString["ID"] != null && LTP.Common.CheckInput.CheckGUID(Request.QueryString["ID"].ToString()))
            {
                lblID.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["ID"].ToString());

                CJJCH.Model.T_NET_Standards model = new Model.T_NET_Standards();
                model = new BLL.T_NET_Standards().StandardsModel(new Guid(lblID.Text));
                if (model != null)
                {
                    txtDataTitle.Text = model.DataTitle;
                    txtDataServiceID.Text = model.DataServiceID;
                    txtDataName.Text = model.DataName;
                    dropStandardsType.SelectedValue = model.ThirdID.ToString();
                    txtDataAuthor.Text = model.DataAuthor;
                    txtDataSource.Text = model.DataSource;
                    txtPlusDate.Text = DateTime.Parse(model.PlusDate.ToString()).ToString("yyyy/MM/dd");
                    txtDataAbout.Text = model.DataAbout;
                    txtContent.Value = model.DataNote;
                    txtTrans.Text = model.OrderNum.ToString();
                }
            }
            else
            {
                LTP.Common.WindowClass.WindowBack("操作错误！");
            }
        }
        protected void InitControl()
        {
            if (Request.QueryString["secondid"] != null && LTP.Common.CheckInput.CheckDigit(Request.QueryString["secondid"]))
            {

                CJJCH.Model.T_NET_MenuSecond model = new Model.T_NET_MenuSecond();
                model = new CJJCH.BLL.T_NET_MenuSecond().GetModel(Convert.ToInt32(Request.QueryString["secondid"]));
                if (model != null)
                {
                    lblSecondId.Text = LTP.Common.StringClass.EncodeString(model.ID.ToString());
                    lblSecondName.Text = LTP.Common.StringClass.EncodeString(model.MenuName);
                    lblFirstID.Text = LTP.Common.StringClass.EncodeString(model.FirstID.ToString());
                }
                else
                {
                    LTP.Common.WindowClass.WindowBack("操作错误！");
                }
                if (model.HaveChild == true)
                {
                    IList<CJJCH.Model.T_NET_MenuThird> list = new List<CJJCH.Model.T_NET_MenuThird>();
                    list = (new CJJCH.BLL.T_NET_MenuThird()).GetDrop(Convert.ToInt32(lblSecondId.Text));
                    int typeCount = list.Count;
                    if (typeCount > 0)
                    {
                        for (int i = 0; i < typeCount; i++)
                        {
                            dropStandardsType.Items.Add(new ListItem(list[i].MenuName, list[i].ID.ToString()));
                        }
                        list = null;
                    }
                }
                else
                {
                    type.Style.Add("display", "none");
                }
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string error = checkInput();
            if (error == "")
            {
                CJJCH.Model.T_NET_Standards model = new Model.T_NET_Standards();
                model.DataTitle = LTP.Common.StringClass.EncodeString(txtDataTitle.Text);
                model.DataServiceID = LTP.Common.StringClass.EncodeString(txtDataServiceID.Text);
                model.DataName = LTP.Common.StringClass.EncodeString(txtDataName.Text);
                if (dropStandardsType.SelectedValue != "")
                {
                    model.ThirdID = Convert.ToInt32(dropStandardsType.SelectedValue);
                }
                else 
                {
                    model.ThirdID = 0;
                }
                model.DataAuthor = LTP.Common.StringClass.EncodeString(txtDataAuthor.Text);
                model.DataSource = LTP.Common.StringClass.EncodeString(txtDataSource.Text);
                try
                {
                    model.PlusDate = DateTime.Parse(this.txtPlusDate.Text.Trim());
                }
                catch{ }
                model.DataAbout = LTP.Common.StringClass.RemoveSQL(txtDataAbout.Text);
                model.DataNote = LTP.Common.StringClass.RemoveSQL(txtContent.Value);
                model.OrderNum = Convert.ToInt32(txtTrans.Text);
                model.ManagerID = SessionManagerID;
                model.BuildDate = DateTime.Now;
                model.SecondID = Convert.ToInt32(lblSecondId.Text);
                model.FirstID = Convert.ToInt32(lblFirstID.Text);
                model.ID = new Guid(lblID.Text);

                error = new CJJCH.BLL.T_NET_Standards().EditStandar(model);
                if (error == "")
                {
                    LTP.Common.WindowClass.WindowGoHref("更新成功！", "StandardsList.aspx?curpage=" + LTP.Common.StringClass.EncodeUrl(lblcurpage.Text.Trim())
                        + "&keyword=" + LTP.Common.StringClass.EncodeUrl(lblKeyword.Text.Trim())
                        + "&orderkey=" + LTP.Common.StringClass.EncodeUrl(lblOrderKey.Text.Trim())
                        + "&ordertype=" + LTP.Common.StringClass.EncodeUrl(lblOrderType.Text.Trim())
                        + "&keytype=" + LTP.Common.StringClass.EncodeUrl(lblKeytype.Text.Trim())
                        + "&betime=" + LTP.Common.StringClass.EncodeUrl(lblTimeBegin.Text.Trim())
                        + "&edtime=" + LTP.Common.StringClass.EncodeUrl(lblTimeOver.Text.Trim())
                        + "&secondid=" + LTP.Common.StringClass.EncodeUrl(lblSecondId.Text.Trim())
                        + "&firstid=" + LTP.Common.StringClass.EncodeUrl(lblFirstID.Text.Trim())
                        + "&secondname=" + LTP.Common.StringClass.EncodeUrl(lblSecondName.Text.Trim()));
                }
                else
                {
                    LTP.Common.WindowClass.WindowBack(error);
                }
            }
            else
            {
                LTP.Common.WindowClass.WindowBack(error);
            }
        }
        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("StandardsList.aspx?curpage=" + LTP.Common.StringClass.EncodeUrl(lblcurpage.Text.Trim())
                        + "&keyword=" + LTP.Common.StringClass.EncodeUrl(lblKeyword.Text.Trim())
                        + "&orderkey=" + LTP.Common.StringClass.EncodeUrl(lblOrderKey.Text.Trim())
                        + "&ordertype=" + LTP.Common.StringClass.EncodeUrl(lblOrderType.Text.Trim())
                        + "&keytype=" + LTP.Common.StringClass.EncodeUrl(lblKeytype.Text.Trim())
                        + "&betime=" + LTP.Common.StringClass.EncodeUrl(lblTimeBegin.Text.Trim())
                        + "&edtime=" + LTP.Common.StringClass.EncodeUrl(lblTimeOver.Text.Trim())
                        + "&secondid=" + LTP.Common.StringClass.EncodeUrl(lblSecondId.Text.Trim())
                        + "&firstid=" + LTP.Common.StringClass.EncodeUrl(lblFirstID.Text.Trim())
                        + "&secondname=" + LTP.Common.StringClass.EncodeUrl(lblSecondName.Text.Trim()));
        }
        protected string checkInput()
        {
            StringBuilder error = new StringBuilder();
            if (this.txtDataTitle.Text.Trim() == "")
            {
                error.Append("请输入标准规范编号！ \\n");
            }

            if (this.txtDataServiceID.Text.Trim() == "")
            {
                error.Append("请输入备案号！ \\n");
            }
            if (type.Style["display"] != "none")
            {
                if (this.dropStandardsType.SelectedValue == "")
                {
                    error.Append("请选择所属类别！ \\n");
                }
                else if (!LTP.Common.CheckInput.CheckDigit(this.dropStandardsType.SelectedValue))
                {
                    error.Append("请选择正确的所属类别！ \\n");
                }
            }
            if (this.txtDataName.Text.Trim() == "")
            {
                error.Append("请输入标准规范名称！ \\n");
            }
            if (this.txtDataAuthor.Text.Trim() == "")
            {
                error.Append("请输入主编部门！ \\n");
            }
            if (this.txtPlusDate.Text.Trim() == "")
            {
                error.Append("请输入实行日期	！ \\n");
            }
            else if (!LTP.Common.CheckInput.CheckDateTime(txtPlusDate.Text))
            {
                error.Append("请输入正确的实行日期！\\n");
            }
            if (this.txtDataSource.Text.Trim() == "")
            {
                error.Append("请输入被代替编号！ \\n");
            }
            if (this.txtContent.Value == "")
            {
                error.Append("请输入详情！ \\n");
            }
            if (this.txtTrans.Text == "")
            {
                error.Append("请输入显示排序！ \\n");
            }
            else if (!LTP.Common.CheckInput.CheckDigit(this.txtTrans.Text.Trim()))
            {
                error.Append("请输入正确的显示排序！ \\n");
            }
            return error.ToString();
        }

    }
}